package com.kanshu.ksgb.fastread.doudou.module.book.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dl7.recycler.helper.RecyclerViewHelper;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.base.basemvp.IGenrialMvpView;
import com.kanshu.ksgb.fastread.doudou.base.baseui.BaseFragment;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.view.EmptyLayout;
import com.kanshu.ksgb.fastread.doudou.module.book.adapter.CategoryAdapter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.SelectedCategoryBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.presenter.BookCityPresenter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.utils.BookCityUtils;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * 分类页面
 */
public class CategoryFragment extends BaseFragment implements IGenrialMvpView<BaseResult<List<SelectedCategoryBean>>> {
    @BindView(R.id.recyler_view)
    RecyclerView mRecylerView;
    @BindView(R.id.empty_layout)
    EmptyLayout mEmptyLayout;
    List<SelectedCategoryBean> mList = new ArrayList<>();
    CategoryAdapter mAdapter;
    @BindView(R.id.swipe_refresh)
    TwinklingRefreshLayout mSwipeRefresh;
    Unbinder mUnbinder;
    BookCityPresenter mPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = new BookCityPresenter(lifeCyclerSubject);
        mPresenter.setCategoryView(this);
    }

    public static CategoryFragment newInstance() {
        CategoryFragment fragment = new CategoryFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_common_refresh_rv, null, false);
        mUnbinder = ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        mSwipeRefresh.setPureScrollModeOn();
        mEmptyLayout.setRetryListener(() -> getData());
        mAdapter = new CategoryAdapter(getActivity(), mList);
        mAdapter.addFooterView(BookCityUtils.addEmptyFooter(getActivity(), DisplayUtils.dip2px(getActivity(), 18), 0));
        RecyclerViewHelper.initRecyclerViewV(getActivity(), mRecylerView, false, mAdapter);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getData();
    }

    private void getData() {
        mPresenter.getAllCategories();
    }


    @Override
    public void showLoading(String s) {
        mEmptyLayout.setEmptyStatus(EmptyLayout.STATUS_LOADING);
    }

    @Override
    public void dismissLoading() {
        if (mEmptyLayout != null) {
            mEmptyLayout.hide();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPresenter.detachView();
        mUnbinder.unbind();
    }


    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(getActivity());
        MobclickAgent.onPageStart(getClass().getSimpleName());
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(getActivity());
        MobclickAgent.onPageEnd(getClass().getSimpleName());
    }

    @Override
    public void showContent(BaseResult<List<SelectedCategoryBean>> result) {
        dismissLoading();
        mList.clear();
        mList.addAll(result.result.data);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void showError(int errCode, String errDesc) {
        dismissLoading();
        if (errCode == Constants.ErrCode.NetErr) {
            mEmptyLayout.setEmptyStatus(EmptyLayout.STATUS_NO_NET);
        } else if (errCode == Constants.ErrCode.NoData) {
            mEmptyLayout.setEmptyStatus(EmptyLayout.STATUS_NO_DATA);
        } else {
            mEmptyLayout.setEmptyStatus(EmptyLayout.STATUS_NO_NET);
        }

    }
}